<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="75px">
            <el-form-item label="类别" prop="classifyId">
                <!-- <el-input v-model="queryParams.classifyId" placeholder="请输入类别" clearable
                    @keyup.enter="handleQuery" /> -->
                <el-select v-model="queryParams.classifyId" placeholder="请选择商品库名称" clearable @change="handleQuery" filterable>
                    <el-option v-for="dict in categoryList" :key="dict.id" :label="dict.classifyName"
                        :value="dict.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="名称" prop="productName">
                <el-input v-model="queryParams.productName" placeholder="请输入名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="宽度/门幅" prop="productWidth">
                <el-input v-model="queryParams.productWidth" placeholder="请输入宽度/门幅" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="长度" prop="productLength">
                <el-input v-model="queryParams.productLength" placeholder="请输入长度" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="克重" prop="gramWeight">
                <el-input v-model="queryParams.gramWeight" placeholder="请输入克重" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="颜色" prop="fabricColor">
                <el-input v-model="queryParams.fabricColor" placeholder="请输入颜色" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
                    <el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="品牌" prop="brand">
                <el-input v-model="queryParams.brand" placeholder="请输入品牌" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button size="small" type="primary" plain icon="Plus" @click="handleAdd"
                    v-hasPermi="['manage:Product:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button size="small" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['manage:Product:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button size="small" type="warning" plain icon="Download" @click="handleExport"
                    v-hasPermi="['manage:Product:export']">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="ProductList" @selection-change="handleSelectionChange" border>
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="编号" align="center" type="index" width="60" />
            <el-table-column label="商品名称" align="center" prop="productName" width="120" />
                
            <el-table-column label="商品类别" align="center" prop="classifyName" />
            <el-table-column label="商品规格" prop="productWidth" width="120" align="center">
                <template #default="scope">
                    <span>{{ `${scope.row.productLength} * ${scope.row.productWidth} * ${scope.row.productHeight} `
                        }}</span>
                </template>
            </el-table-column>
            <el-table-column label="商品宽度(cm)" align="center" prop="productWidth" width="120" />
            <el-table-column label="商品长度(cm)" align="center" prop="productLength" width="120" />
            <el-table-column label="商品高度(cm)" align="center" prop="productHeight" width="120" />
            <el-table-column label="商品克重(g)" align="center" prop="gramWeight" width="120" />
            <el-table-column label="商品颜色" align="center" prop="fabricColor" />
            <el-table-column label="商品库存量" align="center" prop="storageQuantity" width="120">
                <template #default="scope">
                    <span>{{ `${scope.row.storageQuantity} ${scope.row.productUnit}` }}</span>
                </template>
            </el-table-column>
            <el-table-column label="商品进货总量" align="center" prop="storageallQuantity" width="120" />
            <el-table-column label="商品出货总量" align="center" prop="shipmentQuantity" width="120" />
            <el-table-column label="状态" align="center" prop="status" />
            <el-table-column label="创建人" align="center" prop="createName" />
            <el-table-column label="备注" align="center" prop="remarks" />
            <el-table-column label="品牌" align="center" prop="brand" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
                <template #default="scope">
                    <el-link icon="Edit" class="table_link_btn" type="primary" :underline="false"
                        @click="handleUpdate(scope.row)" v-hasPermi="['manage:content:edit']"><span
                            class="table_link_text">修改</span></el-link>
                    <el-link icon="Delete" class="table_link_btn" type="danger" :underline="false"
                        @click="handleDelete(scope.row)" v-hasPermi="['manage:content:edit']"><span
                            class="table_link_text">删除</span></el-link>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改商品对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="ProductRef" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="商品类别" prop="classifyId">
                    <!-- <el-input v-model="form.classifyId" placeholder="请输入类别" /> -->
                    <el-select v-model="form.classifyId" placeholder="请选择商品类别" clearable filterable>
                        <el-option v-for="dict in categoryList" :key="dict.id" :label="dict.classifyName"
                            :value="dict.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="商品名称" prop="productName">
                    <el-input v-model="form.productName" placeholder="请输入商品名称" />
                </el-form-item>
                <el-form-item label="商品宽度" prop="productWidth">
                    <el-input-number v-model="form.productWidth" controls-position="right" :min="0" class="w-300" />
                    <span class="ml-10px">cm</span>
                </el-form-item>
                <el-form-item label="商品长度" prop="productLength">
                    <el-input-number v-model="form.productLength" controls-position="right" :min="0" class="w-300" />
                    <span class="ml-10px">cm</span>

                </el-form-item>
                <el-form-item label="商品高度" prop="productHeight">
                    <el-input-number v-model="form.productHeight" controls-position="right" :min="0" class="w-300" />
                    <span class="ml-10px">cm</span>

                </el-form-item>
                <el-form-item label="商品重量/克重" prop="gramWeight">
                    <el-input-number v-model="form.gramWeight" controls-position="right" :min="0" class="w-300" />
                    <span class="ml-10px">g</span>


                </el-form-item>
                <el-form-item label="商品颜色" prop="fabricColor">
                    <el-input v-model="form.fabricColor" placeholder="请输入商品颜色" />
                </el-form-item>
                <el-form-item label="商品库存量" prop="storageQuantity">
                    <el-input v-model="form.storageQuantity" placeholder="请输入库存量" />
                </el-form-item>
                <el-form-item label="商品库存单位" prop="productUnit">
                    <el-select v-model="form.productUnit" placeholder="请选择商品单位" clearable filterable>
                        <el-option v-for="dict in rw_unit_list" :key="dict.value" :label="dict.label"
                            :value="dict.label" />
                    </el-select>
                </el-form-item>
                <!-- <el-form-item label="进货总量" prop="storageallQuantity">
                    <el-input v-model="form.storageallQuantity" placeholder="请输入进货总量" />
                </el-form-item>
                <el-form-item label="出货总量" prop="shipmentQuantity">
                    <el-input v-model="form.shipmentQuantity" placeholder="请输入出货总量" />
                </el-form-item> -->
                <el-form-item label="供应商" prop="brand">
                    <el-input v-model="form.brand" placeholder="请选择供应商" />
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="form.status">
                        <el-radio v-for=" dict in sys_normal_disable " :key="dict.value" :label="dict.value">{{
            dict.label
        }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注" prop="remarks">
                    <el-input v-model="form.remarks" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Product" lang="ts">
import { listCategory } from '../rwcategory/api/category';
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from './api/product';
import { getCurrentInstance, ref } from 'vue';


const { proxy } = getCurrentInstance() as any;
const { sys_normal_disable, rw_unit_list } = proxy.useDict('sys_normal_disable', 'rw_unit_list');

const ProductList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref('');


const form = ref<any>({});
const queryParams = ref({
	pageNum: 1,
	pageSize: 10,
	classifyId: '', productName: '', productWidth: null, productLength: null, gramWeight: null, fabricColor: null, status: '', brand: null,
});
const rules = ref({});

/** 查询商品列表 */
function getList() {
	loading.value = true;
	listProduct(queryParams.value).then((response: any) => {
		ProductList.value = response.data.list;
		total.value = response.data.total;
		loading.value = false;
	});
}
// 获取其他数据:  分类
const categoryList = ref<any[]>([]);
async function getOther() {
	const res = await listCategory({});
	categoryList.value = res.data.list;
	console.log(categoryList,'---');
}
// 取消按钮

function cancel() {
	open.value = false;
	reset();
}

// 表单重置
function reset() {
	form.value = {
		id: null, classifyId: '', productName: '', productWidth: 1, productLength: 1, productHeight: 1, storageQuantity: 0, productUnit: '卷', gramWeight: 0, fabricColor: null, brand: null, storageallQuantity: 0, shipmentQuantity: 0, status: '0', remarks: null,
	};
	proxy.resetForm('ProductRef');
}

/** 搜索按钮操作 */
function handleQuery() {
	queryParams.value.pageNum = 1;
	getList();
}
const queryRef = ref();
/** 重置按钮操作 */
function resetQuery() {
	proxy.resetForm(queryRef);
	handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection: any) {
	ids.value = selection.map((item: any) => item.id);
	single.value = selection.length !== 1;
	multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
	reset();
	open.value = true;
	title.value = '添加商品';
}

/** 修改按钮操作 */
function handleUpdate(row: any) {
	reset();
	const id = row.id || ids.value;
	getProduct(id).then((response: any) => {
		form.value = response.data;
		open.value = true;
		title.value = '修改商品';
	});
}

/** 提交按钮 */
function submitForm() {
	proxy.$refs['ProductRef'].validate((valid: boolean) => {
		if (valid) {
			if (form.value.id != null) {
				updateProduct(form.value).then(response => {
					proxy.$modal.msgSuccess('修改成功');
					open.value = false;
					getList();
				});
			} else {
				addProduct(form.value).then(response => {
					proxy.$modal.msgSuccess('新增成功');
					open.value = false;
					getList();
				});
			}
		}
	});
}

/** 删除按钮操作 */
function handleDelete(row: any) {
	const res = row.id || ids.value;
	proxy.$modal.confirm('是否确认删除商品编号为"' + res + '"的数据项？').then(function () {
		return delProduct(res);
	}).then(() => {
		getList();
		proxy.$modal.msgSuccess('删除成功');
	}).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
	proxy.download('rwmanage/product/exportByStream', {
		...queryParams.value
	}, `Product_${new Date().getTime()}.xlsx`);
}

getList();
getOther();
</script>